home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / Fraction.h < prev    next >
C/C++ Source or Header  |  1990-05-19  |  3KB  |  82 lines

  1. #ifndef    FRACTION_H
  2. #define    FRACTION_H
  3.  
  4. /*$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Fraction.h,v 3.0 90/05/20 00:19:40 kgorlen Rel $*/
  5.  
  6. /* Fraction.h -- declarations for fractions
  7.  
  8.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  9.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  10.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  11.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  12.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  13.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  14.  
  15. Author:
  16.     K. E. Gorlen
  17.     Computer Systems Laboratory, DCRT
  18.     National Institutes of Health
  19.     Bethesda, MD 20892
  20.  
  21. $Log:    Fraction.h,v $
  22.  * Revision 3.0  90/05/20  00:19:40  kgorlen
  23.  * Release for 1st edition.
  24.  * 
  25. */
  26.  
  27. #include "Object.h"
  28.  
  29. class Fraction: public VIRTUAL Object {
  30.     DECLARE_MEMBERS(Fraction);
  31. public:            // static member functions
  32.     static int gcd(int uu, int vv);
  33. private:
  34.     int n,d;
  35.     Fraction(int num, int den, int dum) {
  36.         n = (dum,num); d = den;
  37.     }
  38.     void parseFraction(istream&);
  39.     void reduce();
  40. protected:        // storer() functions for object I/O
  41.     virtual void storer(OIOofd&) const;
  42.     virtual void storer(OIOout&) const;
  43. public:
  44.     Fraction(int num =0, int den =1);
  45.     Fraction(double);
  46.     Fraction(istream&);
  47.     operator double() const        { return (double)n/d; }
  48.     int denominator() const        { return d; }
  49.     int numerator() const        { return n; }
  50.     
  51.     friend Fraction    operator+(const Fraction&, const Fraction&);
  52.     friend Fraction    operator-(const Fraction& u)  { return Fraction(-u.n,u.d); }
  53.     friend Fraction operator-(const Fraction&, const Fraction&);
  54.     friend Fraction operator*(const Fraction&, const Fraction&);
  55.     friend Fraction operator/(const Fraction&, const Fraction&);
  56.     friend bool    operator<(const Fraction& u, const Fraction& v);
  57.     friend bool    operator>(const Fraction& u, const Fraction& v)        { return v<u; }
  58.     friend bool    operator<=(const Fraction& u, const Fraction& v);
  59.     friend bool    operator>=(const Fraction& u, const Fraction& v)    { return v<=u; }
  60.     friend bool    operator==(const Fraction& u, const Fraction& v)     { return u.n == v.n && u.d == v.d; }
  61.     friend bool    operator!=(const Fraction& u, const Fraction& v)    { return !(u==v); }
  62.     
  63.     void operator+=(const Fraction& u)    { *this = *this + u; }
  64.     void operator-=(const Fraction& u)    { *this = *this - u; }
  65.     void operator*=(const Fraction& u)    { *this = *this * u; }
  66.     void operator/=(const Fraction& u)    { *this = *this / u; }
  67.     
  68.     bool between(const Fraction& min, const Fraction& max) const;
  69.     Fraction max(const Fraction&) const;
  70.     Fraction min(const Fraction&) const;
  71.     
  72.     virtual int compare(const Object&) const;
  73.     virtual void    deepenShallowCopy();    // {}
  74.     virtual unsigned hash() const;
  75.     virtual bool isEqual(const Object&) const;
  76.     virtual void printOn(ostream& strm =cout) const;
  77.     virtual void scanFrom(istream& strm);
  78.     virtual const Class* species() const;
  79. };
  80.  
  81. #endif
  82.